﻿Imports System.ComponentModel
Imports System.Text
Imports Newtonsoft.Json.Linq
Imports ToolGood.Words.Pinyin

Public Class FrmSetting
    Private IsEdit As Boolean = False
    Private IsInit As Boolean = True
    Public LastSelect As Integer '记录列表框的当前选中项
    Public JsonSetting As JArray
    Public IconFile() As Bitmap '存放更换后的图标
    Public JsonBackup As JArray

    Private Sub FrmSetting_Load(sender As Object, e As EventArgs) Handles Me.Load
        Icon = FrmMain.Icon

        ChkAutorun.Checked = ConfigData("setting")("autorun")
        ChkUpdate.Checked = ConfigData("setting")("update")
        ChkShowAll.Checked = ConfigData("setting")("showall")
        ChkAlert.Checked = ConfigData("setting")("alert")
        If ConfigData("setting")("sort").ToString = "0" Then
            RdoDisable.Checked = True
        ElseIf ConfigData("setting")("sort").ToString = "1" Then
            RdoFrequency.Checked = True
        ElseIf ConfigData("setting")("sort").ToString = "2" Then
            RdoOrder.Checked = True
        End If
        NudCount.Value = ConfigData("setting")("number")
        ChkClear.Checked = ConfigData("setting")("clear")
        Dim hotkey() As String
        hotkey = Split(Replace(ConfigData("setting")("hotkey").ToString, " ", ""), "+")
        If hotkey.Count = 2 Then
            CmbHotKey1.SelectedIndex = 0
            CmbHotKey2.SelectedItem = hotkey(0)
            TxtHotkey.Text = hotkey(1)
        ElseIf hotkey.Count = 3 Then
            CmbHotKey1.SelectedItem = hotkey(0)
            CmbHotKey2.SelectedItem = hotkey(1)
            TxtHotkey.Text = hotkey(2)
        End If
        If ConfigData("setting")("themes").ToString = "light" Then
            RdoLight.Checked = True
        ElseIf ConfigData("setting")("themes").ToString = "dark" Then
            RdoDark.Checked = True
        End If
        ChkShowTitle.Checked = ConfigData("setting")("bar")
        TrackBar.Value = ConfigData("setting")("transparency")
        LblPercentage.Text = TrackBar.Value.ToString + "%"
        IsInit = False

        Dim i As Integer
        JsonSetting = ConfigData("list").DeepClone
        JsonBackup = ConfigData("list").DeepClone
        ReDim IconFile(JsonSetting.Count - 1)
        If JsonSetting.Count > 0 Then
            ListView.Items.Clear()
            For i = 0 To JsonSetting.Count - 1
                ListView.Items.Add(JsonSetting(i)("name").ToString)
            Next
            ListView.Items(0).Selected = True
        End If
        ToolTip.SetToolTip(TxtHotkey, "请按下一个按键。")
        ToolTip.SetToolTip(TxtCommand, "如果路径中包含空格，需要使用半角引号(" + Chr(34) + ")包裹。\r\n支持添加命令行参数。")

        FrmSetting_SizeChanged(Me, New EventArgs)
    End Sub

    Private Sub FrmSetting_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
        TabControl.Location = New Point(10, 10)
        TabControl.Size = New Size(ClientSize.Width - 20, ClientSize.Height - 20)
        If TabControl.SelectedIndex = 0 Then
            TabPage2.Size = TabControl.SelectedTab.Size
        Else
            TabPage1.Size = TabControl.SelectedTab.Size
        End If

        GrpMiscellaneous.Location = New Point(10, 10)
        GrpMiscellaneous.Size = New Size(TabPage1.ClientSize.Width - 20, ChkAutorun.Height * 2 + 55)
        ChkAutorun.Location = New Point(20, 30)
        ChkUpdate.Location = New Point(295, 30)
        ChkShowAll.Location = New Point(20, ChkAutorun.Bottom + 5)
        ChkAlert.Location = New Point(ChkUpdate.Left, ChkUpdate.Bottom + 5)

        GrpSort.Location = New Point(10, GrpMiscellaneous.Bottom + 10)
        GrpSort.Size = New Size(265, RdoDisable.Bottom - RdoFrequency.Top + 50)
        RdoFrequency.Location = New Point(20, 30)
        RdoOrder.Location = New Point(20, RdoFrequency.Bottom + 5)
        RdoDisable.Location = New Point(20, RdoOrder.Bottom + 5)

        GrpSearch.Location = New Point(GrpSort.Right + 10, GrpSort.Top)
        GrpSearch.Size = New Size(TabPage1.ClientSize.Width - GrpSearch.Left - 10, GrpSort.Height)
        LblCount.Location = New Point(20, 30)
        NudCount.Location = New Point(LblCount.Right + 5, 25)
        ChkClear.Location = RdoDisable.Location

        GrpHotKey.Location = New Point(10, GrpSort.Bottom + 10)
        GrpHotKey.Size = New Size(TabPage1.ClientSize.Width - 20, LblSearch.Height + 50)
        LblSearch.Location = New Point(20, 30)
        CmbHotKey1.Location = New Point(LblSearch.Right + 5, 25)
        LblAnd1.Location = New Point(CmbHotKey1.Right + 5, LblSearch.Top)
        CmbHotKey2.Location = New Point(LblAnd1.Right + 5, 25)
        LblAnd2.Location = New Point(CmbHotKey2.Right + 5, LblSearch.Top)
        TxtHotkey.Location = New Point(LblAnd2.Right + 5, 25)
        BtnHotkeyUse.Location = New Point(TxtHotkey.Right + 10, 25)
        BtnReset.Location = New Point(BtnHotkeyUse.Right + 5, 25)
        LblHotKeyCheck.Location = New Point(BtnReset.Right + 10, LblSearch.Top)

        GrpAppearance.Location = New Point(10, GrpHotKey.Bottom + 10)
        GrpAppearance.Size = New Point(TabPage1.ClientSize.Width - 20, RdoDark.Bottom - RdoLight.Top + 50)
        RdoLight.Location = New Point(20, 30)
        RdoDark.Location = New Point(20, RdoLight.Bottom + 5)
        ChkShowTitle.Location = New Point(295, 30)
        LblTransparency.Location = New Point(ChkShowTitle.Left, ChkShowTitle.Bottom + 5)
        TrackBar.Location = New Point(LblTransparency.Right + 5, LblTransparency.Top)
        LblPercentage.Location = New Point(TrackBar.Right + 5, LblTransparency.Top)

        ListView.Location = New Point(10, 10)
        ListView.Size = New Size(TabPage2.ClientSize.Width - BtnSave.Width * 6 - 55, TabPage2.ClientSize.Height - 20)

        BtnAdd.Location = New Point(ListView.Right + 10, ListView.Top)
        BtnPresupposition.Location = New Point(BtnAdd.Right + 5, ListView.Top)
        BtnDel.Location = New Point(BtnPresupposition.Right + 5, ListView.Top)
        BtnClear.Location = New Point(BtnDel.Right + 5, ListView.Top)
        BtnUp.Location = New Point(BtnAdd.Left, BtnAdd.Bottom + 5)
        BtnDown.Location = New Point(BtnUp.Right + 5, BtnAdd.Bottom + 5)
        BtnLast.Location = New Point(BtnDown.Right + 5, BtnAdd.Bottom + 5)
        BtnNext.Location = New Point(BtnLast.Right + 5, BtnAdd.Bottom + 5)
        BtnRecovery.Location = New Point(BtnNext.Right + 5, BtnAdd.Bottom + 5)
        BtnSave.Location = New Point(BtnRecovery.Right + 5, BtnAdd.Bottom + 5)

        LblName.Location = New Point(BtnUp.Left, BtnUp.Bottom + 50)
        TxtName.Location = New Point(BtnUp.Left, LblName.Bottom + 5)
        LblCommand.Location = New Point(BtnUp.Left, TxtName.Bottom + 10)
        TxtCommand.Location = New Point(BtnUp.Left, LblCommand.Bottom + 5)
        BtnBrowse.Location = New Point(TxtCommand.Right + 5, TxtCommand.Top)
        LblLabel.Location = New Point(BtnUp.Left, TxtCommand.Bottom + 10)
        TxtLabel.Location = New Point(BtnUp.Left, LblLabel.Bottom + 5)
        BtnLabel.Location = New Point(TxtLabel.Right + 5, TxtLabel.Top)

        LblType.Location = New Point(BtnUp.Left, TxtLabel.Bottom + 10)
        RdoApp.Location = New Point(BtnUp.Left, LblType.Bottom + 10)
        RdoSystem.Location = New Point(RdoApp.Left + 120, RdoApp.Top)
        RdoUrl.Location = New Point(RdoSystem.Left + 120, RdoApp.Top)
        RdoFolder.Location = New Point(BtnUp.Left, RdoApp.Bottom + 10)
        RdoExec.Location = New Point(RdoFolder.Left + 120, RdoFolder.Top)
        RdoOther.Location = New Point(RdoExec.Left + 120, RdoFolder.Top)

        LblIcon.Location = New Point(BtnSave.Right - PicIcon.Width - LblIcon.Width, LblName.Top)
        PicIcon.Location = New Point(BtnSave.Right - PicIcon.Width, LblName.Top)
        BtnSysIcon.Location = New Point(LblIcon.Left, PicIcon.Bottom + 10)
        BtnSysIcon.Size = New Size(PicIcon.Right - LblIcon.Left, 24)
        BtnCustomIcon.Location = New Point(LblIcon.Left, BtnSysIcon.Bottom + 5)
        BtnCustomIcon.Size = New Size(PicIcon.Right - LblIcon.Left, 24)
    End Sub

    Private Sub ListView_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView.ItemSelectionChanged
        If e.IsSelected Then
            If LastSelect >= 0 And LastSelect <= ListView.Items.Count - 1 Then
                ListView.Items(LastSelect).BackColor = SystemColors.Window
                ListView.Items(LastSelect).ForeColor = Color.Black
            End If
            ListView.Items(e.ItemIndex).BackColor = SystemColors.MenuHighlight
            ListView.Items(e.ItemIndex).ForeColor = Color.White
            ListView.Items(e.ItemIndex).Focused = True
            LastSelect = e.ItemIndex

            TxtName.Text = JsonSetting(LastSelect)("name")
            TxtLabel.Text = String.Join(",", JsonSetting(LastSelect)("label"))
            TxtCommand.Text = JsonSetting(LastSelect)("command")

            Dim t As String = JsonSetting(LastSelect)("type").ToString
            IsEdit = False
            Select Case t
                Case "0"
                    RdoApp.Checked = True
                    LblCommand.Text = "命令行："
                    BtnBrowse.Enabled = False
                Case "1"
                    RdoSystem.Checked = True
                    LblCommand.Text = "命令行："
                    BtnBrowse.Enabled = False
                Case "2"
                    RdoUrl.Checked = True
                    LblCommand.Text = "网址："
                    BtnBrowse.Enabled = False
                Case "3"
                    RdoFolder.Checked = True
                    LblCommand.Text = "路径："
                    BtnBrowse.Enabled = True
                Case "5"
                    RdoOther.Checked = True
                    LblCommand.Text = "路径："
                    BtnBrowse.Enabled = True
                Case Else
                    RdoExec.Checked = True
                    LblCommand.Text = "命令行："
                    BtnBrowse.Enabled = True
            End Select
            IsEdit = True

            Dim IconPath As String = Application.StartupPath + "\Icon\" + JsonSetting(LastSelect)("icon").ToString
            If JsonSetting(LastSelect)("icon").ToString <> "" And IO.File.Exists(IconPath) Then
                PicIcon.BackgroundImage = LoadImage(Application.StartupPath + "\Icon\" + JsonSetting(LastSelect)("icon").ToString)
            Else
                If IsNothing(IconFile(LastSelect)) Then
                    PicIcon.BackgroundImage = CmdList.GetIcon(JsonSetting(LastSelect)("command").ToString, t)
                Else
                    PicIcon.BackgroundImage = IconFile(LastSelect)
                End If
            End If

            If t = "0" Or t = "1" Then
                TxtCommand.Enabled = False
                RdoUrl.Enabled = False
                RdoFolder.Enabled = False
                RdoExec.Enabled = False
                RdoOther.Enabled = False
                If JsonSetting(LastSelect)("icon").ToString = "" Then
                    BtnSysIcon.Enabled = False
                Else
                    BtnSysIcon.Enabled = True
                End If
            Else
                TxtCommand.Enabled = True
                RdoUrl.Enabled = True
                RdoFolder.Enabled = True
                RdoExec.Enabled = True
                RdoOther.Enabled = True
                BtnSysIcon.Enabled = True
            End If
        End If
    End Sub

    Private Sub ListView_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView.DrawColumnHeader
        Dim sf As New StringFormat
        Try
            ' Store the column text alignment, letting it default to Left if it has not been set to Center or Right.
            Select Case e.Header.TextAlign
                Case HorizontalAlignment.Center
                    sf.Alignment = StringAlignment.Center
                Case HorizontalAlignment.Right
                    sf.Alignment = StringAlignment.Far
            End Select

            sf.LineAlignment = StringAlignment.Center

            ' Draw the standard header background.
            e.DrawBackground()

            ' Draw the header text.
            e.Graphics.DrawString(e.Header.Text, New Font(FontFamily, 12, FontStyle.Bold), Brushes.Black, e.Bounds, sf)
        Finally
            sf.Dispose()
        End Try
    End Sub

    Private Sub ListView_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView.DrawItem
        e.DrawDefault = True
    End Sub

    Private Sub ListView_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView.DrawSubItem
        e.DrawDefault = True
    End Sub

    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
        Dim t() As String = {"新建项目", "XinJianXiangMu", "XJXM"}
        Dim s() As String = t.Clone
        Dim i As Integer
        Dim j As JObject = New JObject
        Dim a As JArray = New JArray
        Do While ListViewExists(ListView, s(0))
            i += 1
            s(0) = t(0) + i.ToString
            s(1) = t(1) + i.ToString
            s(2) = t(2) + i.ToString
        Loop
        ListView.Items.Add(s(0))
        a.Add(String.Join("|", s))
        j("name") = s(0)
        j("label") = a
        j("command") = ""
        j("type") = 4
        j("icon") = ""
        j("count") = 0
        JsonSetting.Add(j)
        JsonBackup.Add(j)
        ReDim Preserve IconFile(ListView.Items.Count - 1)
        ListView.Items(ListView.Items.Count - 1).Selected = True
        ListView.Items(LastSelect).EnsureVisible()
    End Sub

    Private Sub BtnPresupposition_Click(sender As Object, e As EventArgs) Handles BtnPresupposition.Click
        FrmPresupposition.ShowDialog()
    End Sub

    Private Sub BtnDel_Click(sender As Object, e As EventArgs) Handles BtnDel.Click
        If ListView.Items.Count > 0 Then
            ListView.Items(0).Selected = True
            Dim i As Integer
            For i = ListView.Items.Count - 1 To 0 Step -1
                If ListView.Items(i).Checked = True Then
                    ListView.Items(i).Remove()
                    If JsonSetting(i)("icon").ToString <> "" Then
                        IO.File.Delete(Application.StartupPath + "\Icon\" + JsonSetting(i)("icon").ToString)
                    End If
                    JsonSetting(i).Remove()
                    JsonBackup(i).Remove()
                    IconFile = RemoveAt(IconFile, i)
                End If
            Next
            If ListView.Items.Count > 0 Then
                ListView.Items(0).Selected = True
                ListView.Items(0).EnsureVisible()
            Else
                LastSelect = -1
            End If
        End If
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        Dim i As Integer
        If JsonSetting.Count > 0 Then
            For i = 0 To JsonSetting.Count - 1
                If JsonSetting(i)("icon").ToString <> "" Then
                    IO.File.Delete(Application.StartupPath + "\Icon\" + JsonSetting(i)("icon").ToString)
                End If
            Next
        End If
        ListView.Items.Clear()
        JsonSetting = New JArray
        JsonBackup = New JArray
        ReDim IconFile(0)
        LastSelect = -1
    End Sub

    Private Sub BtnUp_Click(sender As Object, e As EventArgs) Handles BtnUp.Click
        If LastSelect > 0 Then
            Dim j As JObject
            Dim i As String
            Dim f As Font
            Dim b As Bitmap
            j = JsonSetting(LastSelect - 1)
            JsonSetting(LastSelect - 1) = JsonSetting(LastSelect)
            JsonSetting(LastSelect) = j
            j = JsonBackup(LastSelect - 1)
            JsonBackup(LastSelect - 1) = JsonBackup(LastSelect)
            JsonBackup(LastSelect) = j
            i = ListView.Items(LastSelect - 1).Text
            ListView.Items(LastSelect - 1).Text = ListView.Items(LastSelect).Text
            ListView.Items(LastSelect).Text = i
            f = ListView.Items(LastSelect - 1).Font
            ListView.Items(LastSelect - 1).Font = ListView.Items(LastSelect).Font
            ListView.Items(LastSelect).Font = f
            ListView.Items(LastSelect - 1).Selected = True
            b = IconFile(LastSelect - 1)
            IconFile(LastSelect - 1) = IconFile(LastSelect)
            IconFile(LastSelect) = b
        End If
    End Sub

    Private Sub BtnDown_Click(sender As Object, e As EventArgs) Handles BtnDown.Click
        If LastSelect < ListView.Items.Count - 1 Then
            Dim j As JObject
            Dim i As String
            Dim f As Font
            Dim b As Bitmap
            j = JsonSetting(LastSelect + 1)
            JsonSetting(LastSelect + 1) = JsonSetting(LastSelect)
            JsonSetting(LastSelect) = j
            j = JsonBackup(LastSelect + 1)
            JsonBackup(LastSelect + 1) = JsonBackup(LastSelect)
            JsonBackup(LastSelect) = j
            i = ListView.Items(LastSelect + 1).Text
            ListView.Items(LastSelect + 1).Text = ListView.Items(LastSelect).Text
            ListView.Items(LastSelect).Text = i
            f = ListView.Items(LastSelect + 1).Font
            ListView.Items(LastSelect + 1).Font = ListView.Items(LastSelect).Font
            ListView.Items(LastSelect).Font = f
            ListView.Items(LastSelect + 1).Selected = True
            b = IconFile(LastSelect + 1)
            IconFile(LastSelect + 1) = IconFile(LastSelect)
            IconFile(LastSelect) = b
        End If
    End Sub

    Private Sub BtnLast_Click(sender As Object, e As EventArgs) Handles BtnLast.Click
        If LastSelect > 0 Then
            ListView.Items(LastSelect).Selected = False
            ListView.Items(LastSelect - 1).Selected = True
            ListView.Items(LastSelect).EnsureVisible()
        End If
    End Sub

    Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
        If LastSelect < ListView.Items.Count - 1 Then
            ListView.Items(LastSelect).Selected = False
            ListView.Items(LastSelect + 1).Selected = True
            ListView.Items(LastSelect).EnsureVisible()
        End If
    End Sub

    Private Sub BtnRecovery_Click(sender As Object, e As EventArgs) Handles BtnRecovery.Click
        If LastSelect < 0 Then Exit Sub
        JsonSetting(LastSelect) = JsonBackup(LastSelect).DeepClone
        ListView.Items(LastSelect).Font = ListView.Font
        ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString
        IconFile(LastSelect) = Nothing
        ListView.Items(LastSelect).Selected = False
        ListView.Items(LastSelect).Selected = True
    End Sub

    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
        Dim i As Integer
        If JsonSetting.Count = 0 Then Exit Sub
        For i = 0 To IconFile.Count - 1
            If IsNothing(IconFile(i)) = False Then
                If JsonBackup(i)("icon").ToString <> "" Then
                    IO.File.Delete(Application.StartupPath + "\Icon\" + JsonBackup(i)("icon").ToString)
                End If
                IO.Directory.CreateDirectory(Application.StartupPath + "\Icon\")
                IconFile(i).Save(Application.StartupPath + "\Icon\" + JsonSetting(i)("icon").ToString, Imaging.ImageFormat.Png)
            End If
        Next

        ConfigData("list") = JsonSetting.DeepClone
        JsonBackup = JsonSetting.DeepClone
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)

        ListView.Items.Clear()
        For i = 0 To JsonSetting.Count - 1
            ListView.Items.Add(JsonSetting(i)("name").ToString)
        Next
        ListView.Items(0).Selected = True
    End Sub

    Private Sub BtnBrowse_Click(sender As Object, e As EventArgs) Handles BtnBrowse.Click
        If RdoFolder.Checked Or RdoOther.Checked Then
            OpenFileDialog.InitialDirectory = Application.StartupPath
            If FolderBrowserDialog.ShowDialog = DialogResult.OK Then
                TxtCommand.Text = Chr(34) + FolderBrowserDialog.SelectedPath + Chr(34)
            End If
        Else
            OpenFileDialog.Filter = "应用程序|*.exe|所有文件|*.*"
            OpenFileDialog.InitialDirectory = Application.StartupPath
            If OpenFileDialog.ShowDialog = DialogResult.OK Then
                TxtCommand.Text = Chr(34) + OpenFileDialog.FileName + Chr(34)
            End If
        End If
    End Sub

    Private Sub BtnSysIcon_Click(sender As Object, e As EventArgs) Handles BtnSysIcon.Click
        If TxtCommand.Text <> "" Then
            If CmdList.IsCommand(TxtCommand.Text) Then
                PicIcon.BackgroundImage = CmdList.GetIcon(TxtCommand.Text)
            Else
                Select Case JsonSetting(LastSelect)("type").ToString
                    Case 2
                        PicIcon.BackgroundImage = My.Resources.internet
                    Case 3
                        PicIcon.BackgroundImage = My.Resources.folder
                    Case 5
                        PicIcon.BackgroundImage = My.Resources.file
                    Case Else
                        Dim strCmd As String = GetCmd(TxtCommand.Text)
                        If strCmd.IndexOf("\") <> -1 Then '命令指定路径的情况
                            If IO.Path.GetExtension(strCmd) = "" Then '没有指定扩展名的情况
                                If IO.File.Exists(strCmd + ".exe") Then
                                    strCmd += ".exe"
                                ElseIf IO.File.Exists(strCmd + ".com") Then
                                    strCmd += ".com"
                                ElseIf IO.File.Exists(strCmd + ".bat") Then
                                    strCmd += ".bat"
                                Else
                                    strCmd = ""
                                End If
                            End If
                        Else '命令未指定路径的情况
                            If IO.Path.GetExtension(strCmd) = "" Then '没有指定扩展名的情况
                                If IO.File.Exists(strCmd + ".exe") Then
                                    strCmd += ".exe"
                                ElseIf IO.File.Exists(strCmd + ".com") Then
                                    strCmd += ".com"
                                ElseIf IO.File.Exists(strCmd + ".bat") Then
                                    strCmd += ".bat"
                                ElseIf IO.File.Exists(Environment.SystemDirectory + "\" + strCmd + ".exe") Then
                                    strCmd = Environment.SystemDirectory + "\" + strCmd + ".exe"
                                ElseIf IO.File.Exists(Environment.SystemDirectory + "\" + strCmd + ".com") Then
                                    strCmd = Environment.SystemDirectory + "\" + strCmd + ".com"
                                ElseIf IO.File.Exists(Environment.SystemDirectory + "\" + strCmd + ".bat") Then
                                    strCmd = Environment.SystemDirectory + "\" + strCmd + ".bat"
                                ElseIf IO.File.Exists(IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd + ".exe") Then
                                    strCmd = IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd + ".exe"
                                ElseIf IO.File.Exists(IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd + ".com") Then
                                    strCmd = IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd + ".com"
                                ElseIf IO.File.Exists(IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd + ".bat") Then
                                    strCmd = IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd + ".bat"
                                Else
                                    strCmd = ""
                                End If
                            Else
                                If IO.File.Exists(strCmd) Then

                                ElseIf IO.File.Exists(Environment.SystemDirectory + "\" + strCmd) Then
                                    strCmd = Environment.SystemDirectory + "\" + strCmd
                                ElseIf IO.File.Exists(IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd) Then
                                    strCmd = IO.Path.GetDirectoryName(Environment.SystemDirectory) + "\" + strCmd
                                Else
                                    strCmd = ""
                                End If
                            End If
                        End If
                        If strCmd = "" Then
                            PicIcon.BackgroundImage = My.Resources.execute
                        Else
                            If IO.File.Exists(strCmd) Then
                                PicIcon.BackgroundImage = Icon.ExtractAssociatedIcon(strCmd).ToBitmap
                            Else
                                PicIcon.BackgroundImage = My.Resources.execute
                            End If
                        End If
                End Select
            End If
            ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
            ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
            IconFile(LastSelect) = PicIcon.BackgroundImage
            JsonSetting(LastSelect)("icon") = UpdateDLL.GetStringMD5(TxtCommand.Text) + ".png"
        End If
    End Sub

    Private Sub BtnCustomIcon_Click(sender As Object, e As EventArgs) Handles BtnCustomIcon.Click
        OpenFileDialog.Filter = "图标文件|*.ico|图像文件|*.bmp;*.gif;*.jpg;*.png|应用程序|*.exe|所有文件|*.*"
        OpenFileDialog.InitialDirectory = Application.StartupPath
        If OpenFileDialog.ShowDialog = DialogResult.OK Then
            ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
            ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
            If IO.Path.GetExtension(OpenFileDialog.FileName).ToLower = ".exe" Then
                PicIcon.BackgroundImage = Icon.ExtractAssociatedIcon(OpenFileDialog.FileName).ToBitmap
            Else
                PicIcon.BackgroundImage = LoadImage(OpenFileDialog.FileName)
            End If
            IconFile(LastSelect) = PicIcon.BackgroundImage
            JsonSetting(LastSelect)("icon") = UpdateDLL.GetStringMD5(TxtName.Text + TxtCommand.Text) + ".png"
            BtnSysIcon.Enabled = True
        End If
    End Sub

    Private Sub TxtName_LostFocus(sender As Object, e As EventArgs) Handles TxtName.LostFocus
        If IsEdit And TxtName.Text <> JsonSetting(LastSelect)("name").ToString Then
            ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
            ListView.Items(LastSelect).Text = TxtName.Text + " [已修改]"
            JsonSetting(LastSelect)("name") = TxtName.Text
            SmartLabel()
        End If
    End Sub

    Private Sub BtnLabel_Click(sender As Object, e As EventArgs) Handles BtnLabel.Click
        FrmLabel.ShowDialog()
    End Sub

    Private Sub TxtCommand_LostFocus(sender As Object, e As EventArgs) Handles TxtCommand.LostFocus
        If IsEdit And TxtCommand.Text <> JsonSetting(LastSelect)("command").ToString Then
            ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
            ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
            JsonSetting(LastSelect)("command") = TxtCommand.Text
            SmartLabel()
        End If
    End Sub

    Private Sub TxtCommand_DragDrop(sender As Object, e As DragEventArgs) Handles TxtCommand.DragDrop
        Try
            Dim filePath() As String = e.Data.GetData(DataFormats.FileDrop)
            TxtCommand.Text = Chr(34) + filePath(0) + Chr(34)
            TxtName.Text = GetCmd(TxtCommand.Text, True)
            RdoExec.Checked = True
            TxtName_LostFocus(Me, New EventArgs)
            TxtCommand_LostFocus(Me, New EventArgs)
            BtnSysIcon_Click(Me, New EventArgs)
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub TxtCommand_DragEnter(sender As Object, e As DragEventArgs) Handles TxtCommand.DragEnter
        Try
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub RdoApp_CheckedChanged(sender As Object, e As EventArgs) Handles RdoApp.CheckedChanged
        If IsEdit And RdoApp.Checked Then
            ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
            ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
            JsonSetting(LastSelect)("type") = 0
        End If
    End Sub

    Private Sub RdoSystem_CheckedChanged(sender As Object, e As EventArgs) Handles RdoSystem.CheckedChanged
        If IsEdit And RdoSystem.Checked Then
            If CType(JsonSetting(LastSelect)("type"), Integer) <> 1 Then
                ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
                ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
                JsonSetting(LastSelect)("type") = 1
            End If
        End If
    End Sub

    Private Sub RdoUrl_CheckedChanged(sender As Object, e As EventArgs) Handles RdoUrl.CheckedChanged
        If IsEdit And RdoUrl.Checked Then
            If CType(JsonSetting(LastSelect)("type"), Integer) <> 2 Then
                ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
                ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
                JsonSetting(LastSelect)("type") = 2
                LblCommand.Text = "网址："
                BtnBrowse.Enabled = False
                If JsonSetting(LastSelect)("icon").ToString = "" Then
                    PicIcon.BackgroundImage = My.Resources.internet
                End If
            End If
        End If
    End Sub

    Private Sub RdoFolder_CheckedChanged(sender As Object, e As EventArgs) Handles RdoFolder.CheckedChanged
        If IsEdit And RdoFolder.Checked Then
            If CType(JsonSetting(LastSelect)("type"), Integer) <> 3 Then
                ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
                ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
                JsonSetting(LastSelect)("type") = 3
                LblCommand.Text = "路径："
                BtnBrowse.Enabled = True
                If JsonSetting(LastSelect)("icon").ToString = "" Then
                    PicIcon.BackgroundImage = My.Resources.folder
                End If
            End If
        End If
    End Sub

    Private Sub RdoExec_CheckedChanged(sender As Object, e As EventArgs) Handles RdoExec.CheckedChanged
        If IsEdit And RdoExec.Checked Then
            If CType(JsonSetting(LastSelect)("type"), Integer) <> 4 Then
                ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
                ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
                JsonSetting(LastSelect)("type") = 4
                LblCommand.Text = "命令行："
                BtnBrowse.Enabled = True
                If JsonSetting(LastSelect)("icon").ToString = "" Then
                    PicIcon.BackgroundImage = My.Resources.execute
                End If
            End If
        End If
    End Sub

    Private Sub RdoOther_CheckedChanged(sender As Object, e As EventArgs) Handles RdoOther.CheckedChanged
        If IsEdit And RdoOther.Checked Then
            If CType(JsonSetting(LastSelect)("type"), Integer) <> 5 Then
                ListView.Items(LastSelect).Font = New Font(FontFamily, 12, FontStyle.Italic)
                ListView.Items(LastSelect).Text = JsonSetting(LastSelect)("name").ToString + " [已修改]"
                JsonSetting(LastSelect)("type") = 5
                LblCommand.Text = "路径："
                BtnBrowse.Enabled = True
                If JsonSetting(LastSelect)("icon").ToString = "" Then
                    PicIcon.BackgroundImage = My.Resources.file
                End If
            End If
        End If
    End Sub

    Private Sub FrmSetting_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        If TabControl.SelectedIndex = 1 Then
            If MsgBox("本页项目修改之后需要手动保存，否则修改将会丢失，确定要关闭吗？", vbInformation + vbOKCancel, "提示") <> vbOK Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Function JArrayToString(j As JArray) As String
        Dim i As Integer
        Dim s As String = ""
        If j.Count > 0 Then
            For i = 0 To j.Count - 1
                s += j(i).ToString + IIf(i = j.Count - 1, "", ",")
            Next
        End If
        Return s
    End Function

    Private Sub ChkAutorun_CheckedChanged(sender As Object, e As EventArgs) Handles ChkAutorun.CheckedChanged
        If IsInit = True Then Exit Sub
        Dim Reg As Microsoft.Win32.RegistryKey
        Dim SubReg As Microsoft.Win32.RegistryKey
        Reg = Microsoft.Win32.Registry.CurrentUser
        SubReg = Reg.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
        If ChkAutorun.Checked Then
            SubReg.SetValue(AppName, Application.ExecutablePath, Microsoft.Win32.RegistryValueKind.String)
            ConfigData("setting")("autorun") = True
        Else
            Try
                SubReg.DeleteValue(AppName)
            Catch ex As Exception
                '
            End Try
            ConfigData("setting")("autorun") = False
        End If
        Reg.Close()
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub ChkUpdate_CheckedChanged(sender As Object, e As EventArgs) Handles ChkUpdate.CheckedChanged
        If IsInit = True Then Exit Sub
        If ChkUpdate.Checked Then
            ConfigData("setting")("update") = True
        Else
            ConfigData("setting")("update") = False
        End If
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub ChkShowAll_CheckedChanged(sender As Object, e As EventArgs) Handles ChkShowAll.CheckedChanged
        If IsInit = True Then Exit Sub
        If ChkShowAll.Checked Then
            ConfigData("setting")("showall") = True
        Else
            ConfigData("setting")("showall") = False
        End If
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub ChkAlert_CheckedChanged(sender As Object, e As EventArgs) Handles ChkAlert.CheckedChanged
        If IsInit = True Then Exit Sub
        If ChkAlert.Checked Then
            ConfigData("setting")("alert") = True
        Else
            ConfigData("setting")("alert") = False
        End If
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub RdoFrequency_CheckedChanged(sender As Object, e As EventArgs) Handles RdoFrequency.CheckedChanged
        If IsInit = True Then Exit Sub
        If RdoFrequency.Checked Then
            ConfigData("setting")("sort") = 1
            IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
        End If
    End Sub

    Private Sub RdoOrder_CheckedChanged(sender As Object, e As EventArgs) Handles RdoOrder.CheckedChanged
        If IsInit = True Then Exit Sub
        If RdoOrder.Checked Then
            ConfigData("setting")("sort") = 2
            IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
        End If
    End Sub

    Private Sub RdoDisable_CheckedChanged(sender As Object, e As EventArgs) Handles RdoDisable.CheckedChanged
        If IsInit = True Then Exit Sub
        If RdoDisable.Checked Then
            ConfigData("setting")("sort") = 0
            IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
        End If
    End Sub

    Private Sub NudCount_ValueChanged(sender As Object, e As EventArgs) Handles NudCount.ValueChanged
        If IsInit = True Then Exit Sub
        ConfigData("setting")("number") = CType(NudCount.Value, Integer)
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub ChkClear_CheckedChanged(sender As Object, e As EventArgs) Handles ChkClear.CheckedChanged
        If IsInit = True Then Exit Sub
        If ChkClear.Checked Then
            ConfigData("setting")("clear") = True
        Else
            ConfigData("setting")("clear") = False
        End If
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub CmbHotKey1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbHotKey1.SelectedIndexChanged
        LblHotKeyCheck.Visible = False
    End Sub

    Private Sub CmbHotKey2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbHotKey2.SelectedIndexChanged
        LblHotKeyCheck.Visible = False
    End Sub

    Private Sub TxtHotkey_KeyUp(sender As Object, e As KeyEventArgs) Handles TxtHotkey.KeyUp
        TxtHotkey.Text = e.KeyCode.ToString
        LblHotKeyCheck.Visible = False
    End Sub

    Private Sub BtnHotkeyUse_Click(sender As Object, e As EventArgs) Handles BtnHotkeyUse.Click
        Dim hotkey() As String
        hotkey = Split(Replace(ConfigData("setting")("hotkey").ToString, " ", ""), "+")
        If hotkey.Count = 2 Then
            If CmbHotKey1.SelectedIndex = 0 And CmbHotKey2.SelectedItem.ToString = hotkey(0) And TxtHotkey.Text = hotkey(1) Then
                Exit Sub
            End If
        ElseIf hotkey.Count = 3 Then
            If CmbHotKey1.SelectedItem.ToString = hotkey(0) And CmbHotKey2.SelectedItem.ToString = hotkey(1) And TxtHotkey.Text = hotkey(2) Then
                Exit Sub
            End If
        End If

        If CmbHotKey1.SelectedIndex = 0 Then
            If RegisterHotKey(Handle, 1, GetKey(CmbHotKey2.SelectedItem.ToString, True), GetKey(hotkey(1))) Then
                ConfigData("setting")("hotkey") = CmbHotKey2.SelectedItem.ToString + " + " + TxtHotkey.Text
                MsgBox("全局热键（" + CmbHotKey2.SelectedItem.ToString + "+" + TxtHotkey.Text + "）注册成功，但需要重新启动本程序才能应用新的快捷键。", vbOKOnly + vbInformation, "提示")
            Else
                LblHotKeyCheck.Visible = True
                Exit Sub
            End If
        Else
            If RegisterHotKey(Handle, 1, GetKey(CmbHotKey1.SelectedItem.ToString, True) + GetKey(CmbHotKey2.SelectedItem.ToString, True), GetKey(TxtHotkey.Text)) Then
                ConfigData("setting")("hotkey") = CmbHotKey1.SelectedItem.ToString + " + " + CmbHotKey2.SelectedItem.ToString + " + " + TxtHotkey.Text
                MsgBox("全局热键（" + CmbHotKey1.SelectedItem.ToString + "+" + CmbHotKey2.SelectedItem.ToString + "+" + TxtHotkey.Text + "）注册成功，但需要重新启动本程序才能应用新的快捷键。", vbOKOnly + vbInformation, "提示")
            Else
                LblHotKeyCheck.Visible = True
                Exit Sub
            End If
        End If

        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub BtnReset_Click(sender As Object, e As EventArgs) Handles BtnReset.Click
        CmbHotKey1.SelectedIndex = 0
        CmbHotKey2.SelectedIndex = 2
        TxtHotkey.Text = Keys.Space.ToString
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub RdoLight_CheckedChanged(sender As Object, e As EventArgs) Handles RdoLight.CheckedChanged
        If IsInit = True Then Exit Sub
        ConfigData("setting")("themes") = "light"
        FrmMain.SetThemes()
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub RdoDark_CheckedChanged(sender As Object, e As EventArgs) Handles RdoDark.CheckedChanged
        If IsInit = True Then Exit Sub
        ConfigData("setting")("themes") = "dark"
        FrmMain.SetThemes()
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub ChkShowTitle_CheckedChanged(sender As Object, e As EventArgs) Handles ChkShowTitle.CheckedChanged
        If IsInit = True Then Exit Sub
        If ChkShowTitle.Checked Then
            ConfigData("setting")("bar") = True
            FrmMain.FormBorderStyle = FormBorderStyle.FixedSingle
        Else
            ConfigData("setting")("bar") = False
            FrmMain.FormBorderStyle = FormBorderStyle.None
        End If
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub TrackBar_Scroll(sender As Object, e As EventArgs) Handles TrackBar.Scroll
        LblPercentage.Text = TrackBar.Value.ToString + "%"
        FrmMain.Opacity = TrackBar.Value / 100
        ConfigData("setting")("transparency") = TrackBar.Value
        IO.File.WriteAllText(ConfigPath, JsonInfo + ConfigData.ToString, Encoding.UTF8)
    End Sub

    Private Sub SmartLabel()
        Dim s(3) As String
        s(0) = TxtName.Text.Replace(",", "").Replace("|", "")
        s(1) = WordsHelper.GetPinyin(s(0))
        s(2) = WordsHelper.GetFirstPinyin(s(0))
        s(3) = GetCmd(TxtCommand.Text, True)
        WordsHelper.ClearCache()

        s = RemoveRpeat(s)
        s = RemoveNull(s)

        JsonSetting(LastSelect)("label")(0) = String.Join("|", s)
        TxtLabel.Text = String.Join(",", JsonSetting(LastSelect)("label"))
    End Sub
End Class
